package cn.csc.module.distribute.dal.mysql.order;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.framework.mybatis.core.query.LambdaUpdateWrapperX;
import cn.csc.module.distribute.controller.admin.order.vo.DistributeOrderExcelVO;
import cn.csc.module.distribute.controller.admin.order.vo.DistributeOrderExportReqVO;
import cn.csc.module.distribute.controller.admin.order.vo.DistributeOrderPageReqVO;
import cn.csc.module.distribute.controller.app.order.vo.AppDistributeOrderReqVO;
import cn.csc.module.distribute.controller.app.order.vo.AppDistributeOrderRespVO;
import cn.csc.module.distribute.dal.dataobject.order.DistributeOrderDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 分销订单记录 Mapper
 *
 * @author liuchuang
 */
@Mapper
public interface DistributeOrderMapper extends BaseMapperX<DistributeOrderDO> {

    default PageResult<DistributeOrderDO> selectPage(DistributeOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DistributeOrderDO>()
                .eqIfPresent(DistributeOrderDO::getUserId, reqVO.getUserId())
                .orderByDesc(DistributeOrderDO::getId));
    }

    default List<DistributeOrderDO> selectList(DistributeOrderExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<DistributeOrderDO>()
                .eqIfPresent(DistributeOrderDO::getUserId, reqVO.getUserId())
                .eqIfPresent(DistributeOrderDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(DistributeOrderDO::getOrderItemId, reqVO.getOrderItemId())
                .eqIfPresent(DistributeOrderDO::getAmount, reqVO.getAmount())
                .eqIfPresent(DistributeOrderDO::getCommissionRate, reqVO.getCommissionRate())
                .betweenIfPresent(DistributeOrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(DistributeOrderDO::getId));
    }
     default List<DistributeOrderDO> selectListByOrderId(List<BigInteger> orderIds) {
        return selectList(new LambdaQueryWrapperX<DistributeOrderDO>()
                .inIfPresent(DistributeOrderDO::getOrderId,orderIds)
                .orderByDesc(DistributeOrderDO::getCreateTime));
    }

    default List<DistributeOrderDO> selectByOrderId(BigInteger orderId) {
        return selectList(new LambdaQueryWrapperX<DistributeOrderDO>()
                .eq(DistributeOrderDO::getOrderId, orderId));
    }
    default List<DistributeOrderDO> selectByOrderItemId(List<BigInteger> orderItemId) {
        return selectList(new LambdaQueryWrapperX<DistributeOrderDO>()
                .in(DistributeOrderDO::getOrderItemId, orderItemId));
    }
    default int updateByOrderId(BigInteger orderId) {
        return update(null,new LambdaUpdateWrapper<DistributeOrderDO>()
                .set(DistributeOrderDO::getOrderStatus, 3)
                .eq(DistributeOrderDO::getOrderId, orderId));
    }
    default int updateByOrderItemId(List<BigInteger> orderItemId) {
        return update(null,new LambdaUpdateWrapper<DistributeOrderDO>()
                .set(DistributeOrderDO::getOrderStatus, 3)
                .set(DistributeOrderDO::getUpdateTime, LocalDateTime.now())
                .in(DistributeOrderDO::getOrderItemId, orderItemId));
    }
    /**
     * App 我的团队列表分页查询订单ID
     * @param reqVO
     * @param page
     * @return
     */
    Page<AppDistributeOrderRespVO> selectAppOrderPage(@Param("req") AppDistributeOrderReqVO reqVO, IPage<AppDistributeOrderRespVO> page);

    /**
     * 根据orderIds 查询订单的总金额，总佣金列表
     * @param param
     * @return
     */
    List<AppDistributeOrderRespVO> selectAmountListByOrderId(Map<String,Object> param);
    /**
     * admin接口，获取分销订单分页列表
     * @param reqVO
     * @param page
     * @return
     */
    Page<AppDistributeOrderRespVO> selectAdminOrderPage(@Param("req") DistributeOrderPageReqVO reqVO, IPage<AppDistributeOrderRespVO> page);

    /**
     * 根据orderIds 查询订单的总金额，总佣金列表
     * @param reqVO
     * @return
     */
    List<DistributeOrderExcelVO>  selectExportList(@Param("req") DistributeOrderPageReqVO reqVO );
    /**
     * 根据orderIds 查询订单的总金额，总佣金列表
     * @param reqVO
     * @return
     */
    List<DistributeOrderDO>  selectOrderList(@Param("req") AppDistributeOrderReqVO reqVO );

}
